package cn.yfj.basic.controller;

import cn.yfj.basic.config.BusinessException;
import cn.yfj.basic.domain.LoginDto;
import cn.yfj.basic.domain.RegisterDto;
import cn.yfj.basic.service.ILoginService;
import cn.yfj.basic.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/login")
public class LoginController {
    @Autowired
    ILoginService loginService;

    @PostMapping("/account")
    public AjaxResult account(@RequestBody LoginDto dto){
        try {
            Map<String, Object> account = loginService.account(dto);
            return AjaxResult.me().setResultobj(account);
        } catch (BusinessException e) {//捕获我们自己的业务异常
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，稍后重试！！");
        }
    }
    /*微信登录*/
    @PostMapping("/wechat")
    public AjaxResult account(@RequestBody Map<String, String> map){
        try {
            return loginService.wechat(map);
        } catch (BusinessException e) {//捕获我们自己的业务异常
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，稍后重试！！");
        }
    }
    /*绑定*/
    @PostMapping("binder/wechat")
    public AjaxResult binder(@RequestBody Map<String, String> map){
        try {
            Map<String, Object> binder = loginService.binder(map);
            return AjaxResult.me().setResultobj(binder);
        } catch (BusinessException e) {//捕获我们自己的业务异常
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，稍后重试！！");
        }
    }
}
